#include "head.h"
#include "cc9d_support.h"
#include "Local_addr.h"
#include "stm32f10x.h"

#define LOCAL_PORT Local_addr_PORT
#ifndef printd
#define printd(...)
#endif

void getLocalAddr() {
	//FFF0.103: / R
	sCc9dPkg send_pkg;
	send_pkg.from_port = LOCAL_PORT;
	send_pkg.to_port = 103;
	send_pkg.remote_addr = 0xfff0;
	send_pkg.datas[0] = 'R';
	cc9d_SendPkg(&send_pkg, 1);
}
uint16 Local_addr = 0xffff;
uint16 Local_nid = 0xffff;
uint8 Local_ch = 0xff;


void localAddr_recvPkg(sCc9dPkg* recv_pkg, uint8 data_len) {
	//收到的数据以r开头，后续地址16b，网络ID16b，信道16b，用printf打印出来
	if (data_len != 7) {
		return;
	}
	if (recv_pkg->datas[0] != 'r') {
		return;
	}
	if (recv_pkg->from_port != 103) {
		return;
	}
	if (recv_pkg->to_port != LOCAL_PORT) {
		return;
	}
	#pragma pack(1)
	struct {
		uint8 cmd;
		uint16 addr;
		uint16 nid;
		uint8 ch;		
	}*ag = (void*)(recv_pkg->datas);
	Local_addr = ag->addr;
	Local_nid = ag->nid;
	Local_ch = ag->ch;
	printd("LAddr: %d, NID: %d, CH: %d\n", ag->addr, ag->nid, ag->ch);
}
